package lumis.service.wsrp.consumer.doui;

import java.util.Properties;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.Source;
import lumis.doui.source.field.ISourceField;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.service.wsrp.consumer.IPortletServiceManager;
import lumis.service.wsrp.consumer.PortletServiceManagerFactory;
import lumis.util.XmlUtil;

/**
 * Action handler to clone portlets.
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ClonePortletProcessActionHandler extends ProcessActionHandler<Source<ISourceField>>
{
	public static enum ActionType
	{
		ClonePortlet, UpdatePortletClone
	}

	public void processAction() throws PortalException
	{
		String actionTypeStr = XmlUtil.readAttributeString("actionType", processActionNode);
		ActionType actionType;
		try
		{
			actionType = ActionType.valueOf(actionTypeStr);
		}
		catch (IllegalArgumentException e)
		{
			throw new UnexpectedException("Action type not found.", e);
		}
		
		switch (actionType)
		{
			case ClonePortlet:
				clonePortlet();
				break;

			case UpdatePortletClone:
				updatePortletClone();
				break;
		}
	}
	
	/**
	 * 
	 * @throws PortalException
	 * @since 6.0.0
	 */
	private void updatePortletClone() throws PortalException
	{
		String portletHandle = getParameter("portletHandle", String.class);
		String producerEntityId = source.getDouiContext().getRequest().getServiceInstanceConfig()
				.getId();

		IPortletServiceManager portletServiceManager = PortletServiceManagerFactory.getPortletServiceManager(producerEntityId);

		String dgNumberOfRowsParamName = "properties.dg.numberOfRows";
		Number numberOfRows = getParameter(dgNumberOfRowsParamName, Number.class);

		Properties properties = getPortletProperties(numberOfRows);

		try
		{
			// updates the cloned portlet properties
			portletServiceManager.setPortletProperties(portletHandle, properties);
		}
		catch (Exception e) {
			if(e instanceof PortalException)
			{
				throw new PortalException(e.getMessage(), getResource());
			}
			else
			{
				throw new UnexpectedException(e);
			}
		}

		addDefaultResponse();
	}

	private void clonePortlet() throws PortalException
	{
		String portletHandle = getParameter("portletHandle", String.class);
		String friendlyName = getParameter("friendlyName", String.class);
		
		String producerEntityId = source.getDouiContext().getRequest().getServiceInstanceConfig().getId();
		IPortletServiceManager portletServiceManager = PortletServiceManagerFactory.getPortletServiceManager(producerEntityId);
		String newPortletHandle = null;
		try{
			
		
	
			newPortletHandle = portletServiceManager.clonePortlet(portletHandle, friendlyName);
			String dgNumberOfRowsParamName = "properties.dg.numberOfRows";
			Number numberOfRows = getParameter(dgNumberOfRowsParamName, Number.class);
	
			Properties properties = getPortletProperties(numberOfRows);
			
			// updates the cloned portlet properties
			portletServiceManager.setPortletProperties(newPortletHandle, properties);
		
		}catch (Exception e) {
			if(e instanceof PortalException)
			{
				if(newPortletHandle != null)
				{
					portletServiceManager.removeClone(newPortletHandle);
					
					throw new PortalException("STR_WSRP_NOT_CLONE_PORTLET;" + portletHandle, getResource());
				}
				else
				{
					throw new PortalException(e.getMessage(), getResource());
				}
				
			}
			else
			{
				throw new UnexpectedException(e);
			}
		}

		addDefaultResponse();
	}

	/**
	 * Creates Properties based on values passed by the user. 
	 * @param numberOfRows
	 * @return {@link Properties} object values passed by user.
	 * @throws PortalException
	 */
	private Properties getPortletProperties(Number numberOfRows)
			throws PortalException {
		Properties properties = new Properties();
		for (int i = 0; i < numberOfRows.intValue(); i++)
		{
			String keyParameterName = "" + (i + 1) + ".key";
			String valueParameterName = "" + (i + 1) + ".value";
			String key = getParameter(keyParameterName, String.class);
			String value = getParameter(valueParameterName, String.class);
			if(key != null && !"".equals(key))
			{
				if(value == null)
					value = "";
				
				properties.setProperty(key, value);				
			}
		}
		return properties;
	}
}